package com.example.common.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.api.Service;
import com.example.common.po.Po;
import io.seata.spring.annotation.GlobalTransactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public abstract class ServiceImpl<T extends Po> implements Service<T> {

    protected static Logger logger = LoggerFactory.getLogger(ServiceImpl.class);

    @Override
    public abstract BaseMapper<T> getMapper();

    @GlobalTransactional
    @Override
    public Integer insert(T entity) {
        return getMapper().insert(entity);
    }

    @GlobalTransactional
    @Override
    public Integer deleteById(Serializable id) {
        return getMapper().deleteById(id);
    }

    @GlobalTransactional
    @Override
    public Integer deleteByMap(Map<String, Object> columnMap) {
        return getMapper().deleteByMap(columnMap);
    }

    @GlobalTransactional
    @Override
    public Integer deleteBatchIds(Collection<? extends Serializable> idList) {
        return getMapper().deleteBatchIds(idList);
    }

    @GlobalTransactional
    @Override
    public Integer updateById(T entity) {
        return getMapper().updateById(entity);
    }

    @Override
    public T selectById(Serializable id) {
        return getMapper().selectById(id);
    }

    @Override
    public List<T> selectBatchIds(Collection<? extends Serializable> idList) {
        return getMapper().selectBatchIds(idList);
    }

    @Override
    public List<T> selectByMap(Map<String, Object> columnMap) {
        return getMapper().selectByMap(columnMap);
    }

    @Override
    public T selectOne(Wrapper<T> wrapper) {
        return getMapper().selectOne(wrapper);
    }

    @Override
    public IPage<T> selectPage(Integer pageNum, Integer pageSize, Wrapper<T> wrapper) {
        IPage<T> page = new Page<>(pageNum, pageSize);
        return getMapper().selectPage(page, wrapper);
    }

    @Override
    public Integer selectCount(Wrapper<T> wrapper) {
        return getMapper().selectCount(wrapper);
    }

    @Override
    public List<T> selectList(Wrapper<T> wrapper) {
        return getMapper().selectList(wrapper);
    }
}